#include <iostream>

using namespace std;

/**
 * 工厂模式
 * 目的
 * 1. 通过统一基类指针访问派生类接口
 * 2. 方便使用
 * 
 * 原理
 * 1. 通过基类和虚函数特性实现
 * 2. 使用一个函数，通过参数类型去创建对应的派生类对象并返回其基类指针
*/

class Machine
{
public:
    Machine() {}
    virtual ~Machine() {}
    virtual void start() {}
    virtual void stop() {}
    virtual void getConfig() {}
};

class MachineA : public Machine
{
public:
    MachineA() {}
    virtual ~MachineA() {}
    virtual void start() { cout << "MachineA::start()\n"; }
    virtual void stop() { cout << "MachineA::stop()\n"; }
    virtual void getConfig() {}
};


class MachineB : public Machine
{
public:
    MachineB() {}
    virtual ~MachineB() {}
    virtual void start() { cout << "MachineB::start()\n"; }
    virtual void stop() { cout << "MachineB::start()\n"; }
    virtual void getConfig() {}
};

Machine* Factory(int type)
{
    Machine* m = nullptr;
    switch (type)
    {
    case 1:
        m = new MachineA;
        break;
    case 2:
        m = new MachineB;
        break;
    
    default:
        break;
    }
    return m;
}

int main()
{
    Machine* pMachine = Factory(1);
    pMachine->start();
    pMachine->stop();
    
    Machine* bMachine = Factory(2);
    bMachine->start();
    bMachine->stop();
    
    return 0;
}